<template>
	<view class="container">
		<view v-if='isReview == false'>
			<video id="video" :src="video" controls autoplay="false" object-fit="fill"
				style='width:100%;height: 90vh;'></video>
			
			<view >
				<view style='padding:10px'>
					<u-button type='success' text='我要参加' shape='circle' @click="toUrl(`/pages/cart/cooperation?id=${productId}`)"></u-button>
				</view>
			</view>
			<!-- 客服 -->
			<view class="fixed-container">
			    <u-button icon='kefu-ermai' text="在线客服" size='small' shape='circle' :plain='true' open-type="contact"></u-button>
			  </view>
		</view>
		<view v-else>
			<img src='https://picture.jmgc.vip/565cea9c-706a-4a87-8d29-aae88a3f3d9b.jpeg' style='width:100%;height:auto' mode='widthFix'/>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				video: '',
				productId: 0,
				isReview: true
			}
		},
		onShow() {
			this.getUpdateManager()
			this.request.get(`api/options/IS_REVIEW`).then((res) => {
				console.log(res.data)
				this.isReview = res.data.value
			})
		},
		onLoad(options) {
			let token = uni.getStorageSync('api_token')
			if (token == '') {
				let _this = this
				uni.login({
					success(result) {
						let data = {
							code: result.code,
						}
						if(typeof(options.recommendedUser) != 'undefined'){
							data.recommendedUser = options.recommendedUser
						}else{
							data.recommendedUser = 2
						}
						if(typeof(options.scene) != 'undefined'){
							data.recommendedUser = options.scene
						}
						_this.request.get(`wechat/mini/login`, data).then((res) => {
							uni.setStorageSync('api_token', res.data.token)
							_this.request.get(`api/user`).then((res) => {
								uni.setStorageSync('myUser',res.data.id)
								_this.request.get(`api/contents/2`).then((res) => {
									console.log(res.data)
									_this.video = res.data.video
									_this.productId = res.data.extraData.productId
								})
							})
						})
					}
				})
			}else{
				this.request.get(`api/contents/2`).then((res) => {
					console.log(res.data)
					this.video = res.data.video
					this.productId = res.data.extraData.productId
				})
			}
		},
		onShareAppMessage(res) {
			let myUser = uni.getStorageSync('myUser')
			return {
				title: '甘链农投',
				desc: '甘链农投',
				path: `/pages/index/index?recommendedUser=${myUser}`,
				imageUrl: '/static/logo.jpg'
			}
		},
		methods: {
			toUrl(url){
				if(url){
					uni.navigateTo({
						url: url
					})
				}
			},
			getUpdateManager() {
				// 检查小程序是否有新版本
				const updateManager = uni.getUpdateManager()
				console.log('update', updateManager)
				updateManager.onCheckForUpdate(function(res) {
					// 请求完新版本信息的回调
					if (res.hasUpdate) {
						// 如果有新版本，则提示用户更新
						uni.showModal({
							title: '更新提示',
							content: '发现新版本，是否更新？',
							success: function(res) {
								if (res.confirm) {
									// 用户点击确定按钮，调用uni.downloadFile()方法下载新版本
									updateManager.onUpdateReady(function() {
										uni.showModal({
											title: '更新提示',
											content: '新版本已经准备好，是否重启应用？',
											success: function(res) {
												if (res.confirm) {
													// 新的版本已经下载好，调用applyUpdate方法重启小程序并应用新版本
													updateManager.applyUpdate()
												}
											}
										})
									})
								} else if (res.cancel) {
									// 用户点击取消按钮，不做任何操作
								}
							}
						})
					}
				})
			},
		}
	}
</script>

<style scoped>
	.fixed-container{
		position: fixed;
		bottom: 10px;
		left: 10px;
	}
</style>